﻿/**
 *      标签驱动相关定义
 */
#ifndef LABEL_DRIVER_DEF_H
#define LABEL_DRIVER_DEF_H
#include <ossThread.h>
#include <ossTime.h>
#include <json/ossJson.h>
#include <ossList.h>
#include <ossString.h>

#define DEVICE_BASE_ID_LEN      30		//本级设备ID的长度
#define DEVICE_ID_MAX			256		//针对多级的设备ID长度


///数据类型定义
typedef enum
{
	LD_BOOT,			//启动与退出信息
	LD_LOG,				//日志信息
    LD_STATUS,			//状态消息
	LD_ALARM,			//告警信息
    LD_DATA,			//数据信息
    LD_REQUEST,			//请求消息
    LD_RESPONSE,		//请求反馈消息   
	LD_RESPONSE_TO_UI,
	LD_RESPONSE_TO_DEV,
	LD_MEMORY,			//内存管理模块信息
	LD_RUNNING_INFO,	//程序运行数据
	LD_UPDATE_UI,		//更新UI消息
    LD_TYPE_COUNT =255,

	//内部使用ID，驱动不可使用的ID
	LD_REQUEST_CLEAR,		//清除所有请求
	LD_CHANGE_DEVICEID,	//改变UI的设备ID

}LdDataType;

typedef enum{
	LD_NONE_STATUS,		//无状	
	LD_INITIALIZING,		//初始化中
	LD_INITIALIZED,		//初始化完毕
	LD_STARTING,			//启动中
	LD_RUNNING,			//运行中
	LD_STOPING,			//停止中
	LD_DEINITIALIZING,	//逆初始化中
	LD_DEINITIALIZED,		//逆初始化完毕
}LdStatus;

/**
 *		设备状态定义
 */
typedef enum
{
	LD_STATUS_NONE,		//未知状态
	LD_STATUS_ONLINE,		//在线状态
	LD_STATUS_OFFLINE,	//离线状态

	LD_STATUS_USER_DEF = 1024,	//超过此值的状态由用户具体定义	
}DeviceStatus;
 
///消息定义
#define REGIST_LISTEN_DATA                          (MSG_USER+7)      //注册监听数据
#define REVOKE_LISTEN_DATA                          (MSG_USER+8)      //撤销监听数据
#define SET_DEVICE_ID_MSG                           (MSG_USER+10)     //设置设备ID消息
#define HANDL_REQUEST_MSG                           (MSG_USER+11)     //处理请求消息,wParam参考“处理请求的消息体(WPARAM)”
#define GET_DEVICE_INSTANCE_MSG						(MSG_USER+12)	  //获取设备句柄
#define REGIST_MEMORY_POOL                          (MSG_USER+14)     //注册内存池
#define REVOKE_MEMORY_POOL                          (MSG_USER+15)     //撤销内存池
#define NOTIFY_REGIST_RESULT_MSG                    (MSG_USER+16)     //告知注册结果的消息
#define IS_REMOTE_DEVICE							(MSG_USER+17)     //判断是否为远程设备
#define IS_MASTER_DEVICE							(MSG_USER+18)     //判断是否为Master设备
#define GET_DEVICE_STATUS_MSG						(MSG_USER+19)     //获取设备状态消息,返回值参考DeviceStatus
#define SEND_LD_DATA_MSG							(MSG_USER + 30)	 //发送数据

#define INVERSE_INIT_DEVICE_REQUEST                 (MSG_USER+44)     //驱动逆初始化消息 请求

#define LD_MSG_MAX								(MSG_USER+10000)	//最大的消息定义，超过本数值可以由其他模块自由定义

#define LD_DEVICE_TSIC_MAX						(0X7FFFFFFF)
#define LD_DEVICE_TSID_START					(0x40000000)//使用这个数字来区分界面请求和设备请求

typedef enum {
	/** fatal */	LOG_PRIORITY_FATAL = 000,
	/** alert */	LOG_PRIORITY_ALERT = 100,
	/** crit */	    LOG_PRIORITY_CRIT = 200,
	/** error */	LOG_PRIORITY_ERROR = 300,
	/** warn */	    LOG_PRIORITY_WARN = 400,
	/** notice */	LOG_PRIORITY_NOTICE = 500,
	/** info */	    LOG_PRIORITY_INFO = 600,
	/** debug */	LOG_PRIORITY_DEBUG = 700,
	/** trace */	LOG_PRIORITY_TRACE = 800,
	/** notset */	LOG_PRIORITY_NOTSET = 900,
	/** unknown */	LOG_PRIORITY_UNKNOWN = 1000
}LogPriority;

class LabelDeviceBase ;


/**
 *      信息上报
 *@dwDataType        消息类型
 *@jsonReportData    上报的数据
 *@return           无
 */
typedef void (LabelDeviceBase::*DeviceReportFun)(LdDataType tDataType,
								OssJson& jsonReportData);


/**
 *		处理设备请求
 */
typedef BOOL(*HandleDeviceRequestFun)(LdDataType tDataType, OssJson& requestParam, LPVOID lpcontext);

typedef struct
{
	LdDataType		tDataType;
    DeviceReportFun fReportFun;
	LabelDeviceBase* recvDev;
}RegistListenData;

/**
 *      设置服务器参数反馈回调
 */
typedef void (*HandRequestResultFun)(OssJson& jsonRequestResult,LPVOID lpContext);


/**
 *      分配内存
 */
typedef void* (*LdMallocFun)(LPVOID lpcontext, LabelDeviceBase*pLabelDevice,int iSize);

/**
 *      分配内存
 *@return	成功释放返回TRUE
 *			否则返回FALSE
 */
typedef BOOL (*LdFreeFun)(LPVOID lpcontext, LabelDeviceBase*pLabelDevice,void* pBuf);

/**
 *		内存分配接口
 */
typedef struct LdMemoryFun_tag{
	OssListNode		node;
	LdMallocFun		mallocFun;
	LdFreeFun		freeFun;
	LPVOID			lpContext;
}LdMemoryFun;

/**
 *		运行数据
 */
typedef struct LdRunningInfo_tag
{
	const char* pcFlag;
	const char* pcFunctionName;
	const char* pcFileName;			//文件名称
	int	iLine;				//所在文件的行数
	const char* pcDeviceId;
	DWORD		dwCallTime;			//调用时间	
	DWORD		dwConsumingTime;	//消耗时间
	DWORD		dwThreadId;			//线程ID
}LdRunningInfo;

#define BEG_CALL_FLAG		"beginCall"
#define END_CALL_FLAG		"endCall"

#define LD_BEG_CALL()	{\
	LdRunningInfo		dri={BEG_CALL_FLAG,(const char*)__FUNCTION__,(const char*)__FILE__,__LINE__,GetDeviceId(),OssGetTickCount(),OssGetCurrentThreadId()}; \
	DeviceReport(LD_RUNNING_INFO,(char*)&dri,sizeof(dri));\
}

#define LD_END_CALL()	{\
	LdRunningInfo		dri={END_CALL_FLAG,(const char*)__FUNCTION__,(const char*)__FILE__,__LINE__,GetDeviceId(),OssGetTickCount(),OssGetCurrentThreadId()}; \
	DeviceReport(LD_RUNNING_INFO,(char*)&dri,sizeof(dri));\
}

/**
 *      labelDriver创建与释放通知
 *@pLabelDevice	通知的实例
 *@bCreate		
 */
typedef void (*LdCreateAndFreeNotify)(LabelDeviceBase* pLabelDevice,BOOL bCreate, LPVOID lpContext);


/**
 *      定义驱动的入口，必须是LabelDriverBase的派生类
 */
#define LABEL_DRIVER_ENTRY(entry) DLL_API OssVersion* DriverGetVersion()\
										{\
											return &g_version;\
										}\
									DLL_API LabelDeviceBase* CreateDriverInstance(LabelDeviceBase *pLabelDevice) \
                                                {\
													entry* pIns = (new entry(pLabelDevice));\
													if (pIns) pIns->SetVersionFun(DriverGetVersion);\
                                                    return pIns;\
                                                }\
								  


#endif